home *** CD-ROM | disk | FTP | other *** search
- Path: fs7.ece.cmu.edu!cxdunn
- From: cxdunn@ece.cmu.edu (Christopher Dunn)
- Newsgroups: comp.lang.smalltalk,comp.lang.perl.misc,comp.lang.pascal.borland,comp.lang.eiffel,comp.lang.cobol,comp.lang.c++.leda,comp.lang.c,comp.lang.basic.visual.3rdparty,alt.computer.workshop.live,alt.comp.hardware.homebuilt,alt.clearing.technology,alt.chinese.computing,news.newusers.questions
- Subject: Re: PROGRAMERS OF ANY LANGUAGE
- Date: Sun, 31 Mar 1996 17:22:55 EST
- Organization: Electrical and Computer Engineering, Carnegie Mellon.
- Distribution: cmu
- Message-ID: <1996Mar31.172255@nova.ece.cmu.edu>
- References: <Pine.SOL.3.91.960329010021.13209A-100000@harvey>
- NNTP-Posting-Host: nova.ece.cmu.edu
- X-newsreader: xrn 7.01
- To: Drew <aellis@harvey>
- Originator: cxdunn@ece.cmu.edu
-
-
- Here is a short report a produced recently to convince some
- people in this department that C++ is not inferior to C:
-
- I hope that this small example settles, once
- and for all, the matter of the differences
- between C and C++ coding.
-
- 1) C compiles much faster. (12 times faster here.)
- 2) C compiles into a much smaller executable.
- (5 times smaller here.)
- 3) C++ requires a SMALLER source file.
- 4) C++ is AT LEAST AS FAST AS C.
-
- The reason for the speed advantage in this
- case is that C uses qsort, which calls a compare
- function via a function pointer. C++ uses sort,
- which is the STL implementation of qsort, and which
- uses the standard < operator.
- If the items being sorted were objects, then
- the STL sort could use an inline version of operator<,
- and the speed advantage of C++ would be similar. There
- would be no function call overhead for comparisons in C++,
- while in C there would still be a function-pointer dereference
- and a function call (with concommitant stack operations).
- The only way for C to catch up to C++ in this
- example is for the qsort routine to be hand-coded, eliminating
- the need for a comparison function. But a primary intent of
- the Standard Template Library is to eliminate the need for
- hand-coding of generic library routines without loss of
- efficiency.
- When people use advanced features of C++ like
- polymorphism (with the "virtual" keyword) they introduce
- inefficiencies into their code, but the equivalent
- C program (which would use various look-up tables) would
- be no faster.
- C++ is not inherently slower than C. With generic
- libraries, C++ is actually faster. I hope this convinces you.
- **************************************************************
- nova:test/timing/ls -l
- total 50
- -rw-r--r-- 1 cxdunn CAD 66 Mar 13 16:52 Makefile
- -rwxr-xr-x 1 cxdunn CAD 3684 Mar 13 16:57 csort*
- -rw-r--r-- 1 cxdunn CAD 491 Mar 13 16:48 csort.c
- -rwxr-xr-x 1 cxdunn CAD 19155 Mar 13 16:57 sort*
- -rw-r--r-- 1 cxdunn CAD 331 Mar 13 16:32 sort.C
- nova:test/timing/time make csort
- cc -O2 csort.c -o csort
- 0.410u 0.160s 0:00.87 65.5% 1295+508k 0+0io 19pf+0w
- nova:test/timing/time make sort
- xlC -O2 -I/afs/ece/usr/cadmisc/C++/include/STL sort.C -o sort
- 5.020u 0.560s 0:10.52 53.0% 1994+1670k 0+0io 455pf+0w
- nova:test/timing/time csort 1000000 123
- 11.680u 0.010s 0:11.68 100.0% 3+3885k 0+0io 0pf+0w
- nova:test/timing/time sort 1000000 123
- 2.630u 0.010s 0:02.63 100.3% 19+3761k 0+0io 0pf+0w
- **************************************************
- A note on the source code:
- The sort() function is the STL version
- of qsort. (The name qsort was already taken, of
- course.)
- I have checked that both programs sort
- correctly. I use random long-integers to
- eliminate differences that might result from
- different choices of a pivot element, and I
- use a random number seed (specified on the
- command-line) to ensure that they sort the
- same list.
- I used no tricks in the C++ code; it is
- as similar to the C code as possible.
- I used -O2 optimization for both. -O3
- gained another 0.1 seconds for the C++ version and
- nothing for the C version, with little change in
- code size, but longer compilation time.
- I found that, for 1,000,000 elements,
- csort takes 0.5s for initialization, while
- sort (C++) takes 0.7s. C++ generally consumes
- more time in initialization, though the
- asymptotic behavior matches or exceeds that
- of C.
-
- C code:
- #include <stdlib.h>
-
- int cmp( const void *vA, const void *vB )
- {
- long int *A = (long int*)vA;
- long int *B = (long int*)vB;
- if (*A < *B) return -1;
- else if (*A > *B) return +1;
- else return 0;
- }
-
- int
- main(int argc, char *argv[])
- {
- int N = atoi(argv[1]);
- int i = N;
- typedef long int * vec;
- vec V = malloc( N*sizeof(long int) );
- vec U = V;
- if (argc>2) srand(atoi(argv[2]));
- while ( i-- )
- *U++ = rand()*rand();
- qsort( V, N, sizeof(long int), cmp );
- return( (int)V[N/4] );
- }
-
- C++ code:
- #include <vector.h>
- #include <algo.h>
- #include <stdlib.h>
-
- int
- main(int argc, char *argv[])
- {
- int N = atoi(argv[1]);
- if (argc>2) srand(atoi(argv[2]));
- int i = N;
- typedef vector<long int> vec;
- vec V(N);
- vec::iterator U = V.begin();
- while ( i-- )
- *U++ = rand()*rand();
- sort( V.begin(), V.end() );
- return( (int)V[N/4] );
- }
-
- --
- Electrical and Computer Engineering
- Carnegie Mellon University
- cxdunn@ece.cmu.edu
- --
- Electrical and Computer Engineering
- Carnegie Mellon University
- cxdunn@ece.cmu.edu
-